package io.geekidea.boot.payment.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.boot.area.dto.AreaDto;
import io.geekidea.boot.area.entity.Area;
import io.geekidea.boot.area.query.AreaQuery;
import io.geekidea.boot.area.service.AreaService;
import io.geekidea.boot.auth.util.LoginUtil;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.payment.dto.PaymentDto;
import io.geekidea.boot.payment.entity.Payment;
import io.geekidea.boot.payment.query.PaymentQuery;
import io.geekidea.boot.payment.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.ml.neuralnet.UpdateAction;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/payment")
@Tag(name="支付管理")
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @PostMapping("/search")
    @Operation(summary = "搜索支付方式",description = "按照多条件组合分页查询支付方式列表")
    public ApiResult search(@RequestBody PaymentQuery paymentQuery){
        Page<Payment> page=new Page<>(paymentQuery.getPage(),paymentQuery.getSize());
        LambdaQueryWrapper<Payment> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(paymentQuery.getPaymentName()), Payment::getPaymentName,paymentQuery.getPaymentName());
        Page<Payment> pageDto=paymentService.page(page,lambdaQueryWrapper);
        PageDto<Payment> pageInfo=new PageDto<>(pageDto.getRecords(),pageDto.getTotal(),pageDto.getSize(),pageDto.getCurrent());
        return ApiResult.success(pageInfo);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询单条数据")
    public ApiResult getPaymentById(@PathVariable(value = "id")Integer id){
        final Payment payment=paymentService.getById(id);
        return ApiResult.success(payment);
    }

    @DeleteMapping("/romove/{id}")
    @Operation(summary = "逻辑删除支付方式")
    public ApiResult removeArea(@PathVariable(value = "id")Integer id){
        paymentService.removeById(id);
        return ApiResult.success("删除支付方式数据成功！");
    }

    @PostMapping("/add")
    public ApiResult addArea(@RequestBody PaymentDto paymentDto) throws Exception {
        if (paymentDto.getStatus().equals("true")) {
            paymentDto.setStatus("0");
        }else if (paymentDto.getStatus().equals("false")) {
            paymentDto.setStatus("1");
        }
        Payment paymentEntity=new Payment();
        BeanUtil.copyProperties(paymentDto,paymentEntity);
        paymentDto.setCreatedBy(LoginUtil.getUsername());
        paymentService.save(paymentEntity);
        return ApiResult.success("添加支付方式数据成功！");
    }

    //逻辑删除
    @GetMapping("/del/{id}")
    @Operation(summary = "物理删除",description = "根据id逻辑删除对应的列")
    public ApiResult del(@PathVariable Integer id){
        boolean update = paymentService.delById(id);
        if (!update) {
            return ApiResult.success("删除失败");
        }
        return ApiResult.success("删除成功");

    }

    @PostMapping("/update")
    public ApiResult updatePayment(@RequestBody PaymentDto paymentDto) throws Exception {
        if (paymentDto.getStatus().equals("true")) {
            paymentDto.setStatus("0");
        }else if (paymentDto.getStatus().equals("false")) {
            paymentDto.setStatus("1");
        }
        Payment paymentEntity = new Payment();
        BeanUtil.copyProperties(paymentDto,paymentEntity);
        paymentDto.setUpdatedBy(LoginUtil.getUsername());
        paymentService.updateById(paymentEntity);
        return ApiResult.success("更改支付方式数据成功！");
    }
}
